package com.example.testrxjava.fragment.code;

import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.RequiresApi;
import android.support.v4.app.ActivityOptionsCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.example.testrxjava.ArticleDetailsActivity;
import com.example.testrxjava.R;
import com.example.testrxjava.activity.search.SearchActivity;
import com.example.testrxjava.adapter.WxArticleSAdapter;
import com.example.testrxjava.base.BaseFragment;
import com.example.testrxjava.util.Action;
import com.example.testrxjava.util.CommUtil;
import com.example.testrxjava.util.ConTract;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadmoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import okhttp3.OkHttpClient;

/**
 * 项目名称：TestRxjava
 *
 * @Author 郭迎辉
 * Create by 14:59 on 2019/4/19.
 * Describe:
 */
public class WxArticleSFragment extends BaseFragment {
    @BindView(R.id.wxarticle_s_Refresh)
    SmartRefreshLayout wxarticle_s_Refresh;
    @BindView(R.id.wxarticle_s_Recyler)
    RecyclerView wxarticle_s_Recyler;



    int page=1;
    private List<WxArticleSBean.DataBean.DatasBean> datasBeans;

    private static WxArticleSFragment instance;
    private int cid;
    private WxArticleSAdapter adapter;
    public static WxArticleSFragment getInstance(int id) {

        Bundle bundle = new Bundle();
        bundle.putInt(ConTract.KEY_ARTICLE_ID, id);
//        if(instance==null){

            instance = new WxArticleSFragment();
//        }
        instance.setArguments(bundle);

        return instance;
    }


    private void getData() {
        Bundle bundle = getArguments();
        if (bundle != null) {
            cid = bundle.getInt(ConTract.KEY_ARTICLE_ID, -1);
            Log.e("cid==",cid+"");
        }
    }
    @Override
    protected void initData() {
        getData();

        Action.getInstance().getWxArticleSList(cid+"", page, new WxArticleSSucessCallback() {
            @Override
            public void wxArticleSSucess(WxArticleSBean bean) {

                datasBeans.addAll(bean.getData().getDatas());
//
                wxarticle_s_Recyler.setLayoutManager(new LinearLayoutManager(getActivity()));
                adapter=new WxArticleSAdapter(R.layout.item_home_list,datasBeans);
                adapter.openLoadAnimation();
                wxarticle_s_Recyler.setAdapter(adapter);
                for (int i = 0; i <datasBeans.size(); i++) {
                    Log.e("wxArticleSSucess: ", datasBeans.get(i).getTitle());
                }



                adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
                    @Override
                    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                        ArticleDetailsActivity.startActivityForResultByFragment(getActivity(),
                                WxArticleSFragment.this,
                                datasBeans.get(position).getLink(),
                                datasBeans.get(position).getTitle(),
                                datasBeans.get(position).getId(),
                                datasBeans.get(position).isCollect(),
                                ConTract.REQUEST_ARTICLE_ACTIVITY);
                    }
                });


            }

            @Override
            public void wxArticleSFail(String message) {
                CommUtil.Toast(getActivity(),"Fail:"+message);
            }
        });

        wxarticle_s_Refresh.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                page=0;
                datasBeans.clear();
                Action.getInstance().getWxArticleSList(cid + "", page, new WxArticleSSucessCallback() {
                    @Override
                    public void wxArticleSSucess(WxArticleSBean bean) {
                        datasBeans.addAll(bean.getData().getDatas());
                        adapter.notifyDataSetChanged();
                    }

                    @Override
                    public void wxArticleSFail(String message) {
                        CommUtil.Toast(getActivity(),"Fail:"+message);
                    }
                });
                refreshlayout.finishRefresh();


            }
        });
        wxarticle_s_Refresh.setOnLoadmoreListener(new OnLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                page++;
                Log.e("page===wx",page+"");
                Log.e("cid===wx",cid+"");
                Action.getInstance().getWxArticleSList(cid + "", page, new WxArticleSSucessCallback() {
                    @Override
                    public void wxArticleSSucess(WxArticleSBean bean) {
                        datasBeans.addAll(bean.getData().getDatas());
                        adapter.notifyDataSetChanged();

                    }

                    @Override
                    public void wxArticleSFail(String message) {
                        CommUtil.Toast(getActivity(),"Fail:"+message);

                    }
                });
                refreshlayout.finishLoadmore();
            }
        });


    }


    @Override
    protected View initView() {
        View view= LayoutInflater.from(getContext()).inflate(R.layout.wxarticle_s_fragment,null);
        datasBeans=new ArrayList<WxArticleSBean.DataBean.DatasBean>();

        return view;
    }
}
